package com.ruoyi.web.controller.honor.student;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.honor.student.GraduateStudent;
import com.ruoyi.system.domain.honor.student.GraduateStudentEducation;
import com.ruoyi.system.domain.honor.student.GraduateStudentVo;
import com.ruoyi.system.service.honor.IGraduateStudentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代性毕业生信息Controller
 * 
 * @author ruoyi
 * @date 2025-08-01
 */
@RestController
@RequestMapping("/honor/student")
public class GraduateStudentController extends BaseController
{
    @Autowired
    private IGraduateStudentService graduateStudentService;

    /**
     * 查询代性毕业生信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(GraduateStudent graduateStudent)
    {
        startPage();
        List<GraduateStudent> list = graduateStudentService.selectGraduateStudentList(graduateStudent);
        return getDataTable(list);
    }

    /**
     * 导出代性毕业生信息列表
     */
    @Log(title = "代性毕业生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GraduateStudent graduateStudent,
                       @RequestParam(required = false) String exportFields)
    {
        List<GraduateStudent> list = graduateStudentService.selectGraduateStudentList(graduateStudent);
        List<GraduateStudentVo> graduateStudentVoList = new java.util.ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for (GraduateStudent graduateStudent1 : list) {
                List<GraduateStudentEducation> educationList = graduateStudent1.getEducationList();
                if (educationList != null && !educationList.isEmpty()) {
                    for ( GraduateStudentEducation education : educationList) {
                        GraduateStudentVo graduateStudentVo = new GraduateStudentVo();
                        graduateStudentVo.setGraduationYear(education.getGraduationYear());
                        graduateStudentVo.setDegreeSchool(education.getDegree());
                        graduateStudentVo.setDegree(graduateStudent1.getDegree());
                        graduateStudentVo.setName(graduateStudent1.getName());
                        graduateStudentVo.setAge(graduateStudent1.getAge());
                        graduateStudentVo.setLearningMethod(graduateStudent1.getLearningMethod());
                        graduateStudentVo.setWorkUnit(graduateStudent1.getWorkUnit());
                        graduateStudentVo.setWorkLocation(graduateStudent1.getWorkLocation());
                        graduateStudentVo.setUnitType(graduateStudent1.getUnitType());
                        graduateStudentVo.setAdministrativeLevel(graduateStudent1.getAdministrativeLevel());
                        graduateStudentVo.setProfessionalTechnicalPost(graduateStudent1.getProfessionalTechnicalPost());
                        graduateStudentVo.setTeacherId(graduateStudent1.getTeacherId());
                        graduateStudentVo.setTeacherName(graduateStudent1.getTeacherName());
                        graduateStudentVo.setRemark(graduateStudent1.getRemark());
                        graduateStudentVoList.add(graduateStudentVo);
                    }
                }
            }
        }
        ExcelUtil<GraduateStudentVo> util = new ExcelUtil<GraduateStudentVo>(GraduateStudentVo.class);
        // 如果传递了导出字段，则进行处理
        if (StringUtils.isNotBlank(exportFields)) {
            // 将逗号分隔的字段转为数组，调用showColumn指定显示字段
            String[] fields = exportFields.split(",");
            util.showColumn(fields); // 关键：使用ExcelUtil已有的showColumn方法
        }
        util.exportExcel(response, graduateStudentVoList, "代性毕业生信息数据");
    }

    /**
     * 获取代性毕业生信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(graduateStudentService.selectGraduateStudentById(id));
    }

    /**
     * 新增代性毕业生信息
     */
    @Log(title = "代性毕业生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GraduateStudent graduateStudent)
    {
        return toAjax(graduateStudentService.insertGraduateStudent(graduateStudent));
    }

    /**
     * 修改代性毕业生信息
     */
    @Log(title = "代性毕业生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GraduateStudent graduateStudent)
    {
        return toAjax(graduateStudentService.updateGraduateStudent(graduateStudent));
    }

    /**
     * 删除代性毕业生信息
     */
    @Log(title = "代性毕业生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(graduateStudentService.deleteGraduateStudentByIds(ids));
    }
}
